const axios = require('axios');
const express = require('express');

const app = express();

app.get('/', async function(req, res) {
    const id = req.query.postId;
    const result = await getPostInfo(id)
    res.writeHead(200,{'Content-Type':'text/html;charset=utf-8',"Access-Control-Allow-Origin":"*"})
    res.end(JSON.stringify(result));
})

app.get('/recommend_all_feed', async (req,res) => {
  const num = Number(req.query.count) || 10
  const result = await getRecommendAllFeed(num)
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8',"Access-Control-Allow-Origin":"*"})
  res.end(JSON.stringify(result));
});

// 前端列表
app.get('/recommend_cate_feed', async (req,res) => {
  const num = Number(req.query.count) || 10
  const result = await getFeCommend(num)
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8',"Access-Control-Allow-Origin":"*"})
  res.end(JSON.stringify(result));
})

// 后端列表
app.get('/recommend_be_feed', async (req,res) => {
  const num = Number(req.query.count) || 10
  const result = await getBeCommend(num)
  // CORS以及编码处理
  res.writeHead(200,{'Content-Type':'text/html;charset=utf-8',"Access-Control-Allow-Origin":"*"})
  res.end(JSON.stringify(result));
})
app.listen(3001, () => {
  console.log('Server run at: ')
  console.log('> Local: \x1B[36m%s\x1B[0m', 'http://localhost:3001/');
});

// 获取所有推荐列表
async function getRecommendAllFeed(num) {
  let arr = new Array()
  return axios.post('https://api.juejin.cn/recommend_api/v1/article/recommend_all_feed', {
    "id_type": 2,
    "client_type": 2608,
    "sort_type": 200,
    "cursor": "0",
    "limit": num
  })
  .then(({ data }) => {
    return data.data
  })
  .then(res => {
    res.forEach(item => {
      if (item.item_type === 2) {
        const { article_id, title } = item.item_info.article_info
        let url = 'https://juejin.cn/post/' + article_id
        item.item_info.article_info.auther_name = item.item_info.author_user_info.user_name
        arr.push(item.item_info.article_info)
      }
    })
    return arr
  })
}

// 获取后端推荐列表
async function getBeCommend(num) {
  let arr = new Array()
  return axios.post('https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed', {
    "id_type":2,
    "sort_type":200,
    "cate_id":"6809637769959178254",
    "cursor":"0",
    "limit":num
  })
  .then(({ data }) => {
    return data.data
  })
}

// 获取前端推荐列表
async function getFeCommend(num) {
  let arr = new Array()
  return axios.post('https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed', {
    "id_type":2,
    "sort_type":200,
    "cate_id":"6809637767543259144",
    "cursor":"0",
    "limit":num
  })
  .then(({ data }) => {
    return data.data
  })
}

// 读取文章内容
async function getPostInfo(id) {
  return axios.post('https://api.juejin.cn/content_api/v1/article/detail', {
    "article_id": id
  }).then(({ data }) => {
    data.data.article_info.auther_name = data.data.author_user_info.user_name
    data.data.article_info.avatar_large = data.data.author_user_info.avatar_large
    return data.data.article_info
  })
}